package com.school.base.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;

import com.school.base.dao.ICourseDao;
import com.school.base.model.Course;
import com.school.common.base.BaseDao;
import com.school.common.util.MapUtils;

/**
 * @author gaobiao
 * 
 */
public class CourseDao extends BaseDao implements ICourseDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.school.base.dao.ICourseDao#getCourseById(int)
	 */
	@Override
	public Course getCourseById(int id) {
		String sql = "select *,s.name subjectName,t.real_name teacherName,cls.name className,a.real_name creatorName from b_course c left join b_subject s on s.id=c.subject_id left join b_teacher t on t.id=c.teacher_id left join b_class cls on cls.id=c.class_id left join b_admin a on a.id=c.creator_id where c.id=?";
		List express = new ArrayList();
		express.add(id);
		SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				query.setParameter(i, express.get(i));
			}
		}
		List list = query.list();
		if (list != null && list.size() > 0) {
			return (Course) MapUtils.mapToBean((HashMap<String, Object>) list.get(0), Course.class);
		}
		return null;
	}

	private String getExpressSql(Course course, List express) {
		String sql = "";
		if (course != null) {
			if (course.getName() != null && !course.getName().isEmpty()) {
				sql += " and c.name like ?";
				express.add("%" + course.getName() + "%");
			}
			if (course.getClassId() > 0) {
				sql += " and c.class_id=?";
				express.add(course.getClassId());
			}
			if (course.getTeacherId() > 0) {
				sql += " and c.teacher_id=?";
				express.add(course.getTeacherId());
			}
		}
		return sql;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.school.base.dao.ICourseDao#getCourseList(com.school.base.model.Course
	 * , int, int)
	 */
	@Override
	public List<Course> getCourseList(Course course, int offset, int pageSize) {
		String sql = "select *,s.name subjectName,t.real_name teacherName,cls.name className,a.real_name creatorName from b_course c left join b_subject s on s.id=c.subject_id left join b_teacher t on t.id=c.teacher_id left join b_class cls on cls.id=c.class_id left join b_admin a on a.id=c.creator_id where 1=1 ";
		List express = new ArrayList();
		sql += this.getExpressSql(course, express);
		SQLQuery query = this.getCurrentSession().createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		if (express != null) {
			for (int i = 0; i < express.size(); i++) {
				query.setParameter(i, express.get(i));
			}
		}
		query.setMaxResults(pageSize);
		query.setFirstResult(offset);
		List list = query.list();
		List<Course> courseList = new ArrayList<Course>();
		for (Object obj : list) {
			Course c = (Course) MapUtils.mapToBean((HashMap<String, Object>) obj, Course.class);
			courseList.add(c);
		}
		return courseList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.school.base.dao.ICourseDao#getTotalCount(com.school.base.model.Course
	 * )
	 */
	@Override
	public int getTotalCount(Course course) {
		String sql = "select count(*) from b_course c left join b_subject s on s.id=c.subject_id left join b_teacher t on t.id=c.teacher_id left join b_class cls on cls.id=c.class_id left join b_admin a on a.id=c.creator_id where 1=1 ";
		List express = new ArrayList();
		sql += this.getExpressSql(course, express);
		return this.getRecordCountBySql(sql, express);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.school.base.dao.ICourseDao#updateCourse(com.school.base.model.Course)
	 */
	@Override
	public void updateCourse(Course course) {
		this.update(course);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.school.base.dao.ICourseDao#deleteCourse(int)
	 */
	@Override
	public void deleteCourse(int id) {
		Course course = new Course();
		course.setId(id);
		this.delete(course);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.school.base.dao.ICourseDao#insertCourse(com.school.base.model.Course)
	 */
	@Override
	public void insertCourse(Course course) {
		course.setCreateTime(new Date());
		this.insert(course);
	}

}
